<template>
	<view class="container" v-if="isloding">
		<view class="top">
			<view class="title">
				当前余额
			</view>
			<view class="num">
				￥{{balance}}
			</view>
		</view>
		<view class="bottom">
			<view class="jine">
				<view class="company">
					￥
				</view>
				<input type="text" v-model="pay_money" placeholder="请输入充值金额" placeholder-class="place"/>
			</view>
			<view class="choice">
				<view class="item" @tap="moneyOne" :class="{moneyS:pay_money==300}">
					￥300.00
				</view>
				<view class="item" @tap="moneyTwo" :class="{moneyS:pay_money==200}">
					￥200.00
				</view>
				<view class="item" @tap="moneyThree" :class="{moneyS:pay_money==100}">
					￥100.00
				</view>
				<view class="item" @tap="moneyFour" :class="{moneyS:pay_money==50}">
					￥50.00
				</view>
				<view class="item" @tap="moneyFive" :class="{moneyS:pay_money==20}">
					￥20.00
				</view>
				<view class="item" @tap="moneySix" :class="{moneyS:pay_money==10}">
					￥10.00
				</view>
			</view>
			<!-- <view class="notes">
				注：充值300赠送XX优惠打折卷
			</view> -->
			<view class="hook" @tap="wxpay">
				<view class="left">
					<image src="../../../static/icon/wechat.png" mode=""></image>
					<view class="title">
						微信支付
					</view>
				</view>
				<view class="right" :class="{active:way==2}"></view>
			</view>
			<view class="hook" @tap="alipay">
				<view class="left">
					<image src="../../../static/alipay.png" mode=""></image>
					<view class="title">
						支付宝支付
					</view>
				</view>
				<view class="right" :class="{active:way==10}"></view>
			</view>
			
		</view>
		<view class="btn" @tap="recharge">
			去支付
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				weichatPay:true,
				// money:300,
				token:'',
				balance:0,
				isloding:false,
				pay_money:300.00,
				way:2,//2微信，10支付宝
			};
		},
		onLoad(e) {
			// console.log(e)
			this.token=e.token
			this.balance=e.balance
			this.isloding=true
		},
		methods:{
			recharge(){
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				if(this.pay_money==''){
					uni.showToast({
						title: '请先选择充值金额',
						icon:'none',
						duration:200
					});
				}else if(this.way==-1){
					uni.showToast({
						title: '必须选择支付方式',
						icon:'none',
						duration:2000
					});
				}else{
					uni.request({
						url: this.webUrl+'/user/my/recharge',
						method: 'POST',
						header:{
							token:this.token
						},
						data: {
							pay_money:this.pay_money,
							way:this.way
						},
						success: pay => {
							console.log(pay)
							uni.hideLoading()
							if(pay.data.code==0){
								uni.request({
									url: this.webUrl+'/index/pay/index/'+pay.data.data.id,
									method: 'GET',
									// data: {
									// 	id:pay.data.data.id
									// },
									success: orderID => {
										console.log(orderID)
										if(orderID.data.code==0){
											if(this.way==2){
												uni.requestPayment({
												    provider: 'wxpay',
												    orderInfo: orderID.data.data, //微信、支付宝订单数据
												    success: function (res) {
												        console.log('success:' + JSON.stringify(res));
														uni.showToast({
															title: '支付成功',
															icon:'none',
															duration:2000
														});
														setTimeout(()=>{
															uni.navigateBack({
																delta: 1
															});
														},1500)
												    },
												    fail: function (err) {
												        console.log('fail:' + JSON.stringify(err));
														// uni.showModal({
														// 	title: '支付失败1',
														// 	content: JSON.stringify(err),
														// 	showCancel: false,
														// 	cancelText: '',
														// 	confirmText: '',
														// 	success: res => {},
														// 	fail: () => {},
														// 	complete: () => {}
														// });
														uni.showToast({
															title: '支付失败',
															icon:'none',
															duration:2000
														});
												    }
												});
											}else if(this.way==10){
												uni.requestPayment({
												    provider: 'alipay',
												    orderInfo: orderID.data.data, //微信、支付宝订单数据
												    success: function (res) {
												        console.log('success:' + JSON.stringify(res));
														uni.showToast({
															title: '支付成功',
															icon:'none',
															duration:2000
														});
														setTimeout(()=>{
															uni.navigateBack({
																delta: 1
															});
														},1500)
												    },
												    fail: function (err) {
												        console.log('fail:' + JSON.stringify(err));
														// uni.showModal({
														// 	title: '支付失败2',
														// 	content: JSON.stringify(err),
														// 	showCancel: false,
														// 	cancelText: '',
														// 	confirmText: '',
														// 	success: res => {},
														// 	fail: () => {},
														// 	complete: () => {}
														// });
														uni.showToast({
															title: '支付失败',
															icon:'none',
															duration:2000
														});
												    }
												});
											}
										}else if(orderID.data.code==3){
											uni.hideLoading()
											uni.showToast({
												title: orderID.data.msg,
												icon:'none',
												duration:2000
											});
										}else{
											uni.showToast({
												title: '网络错误',
												icon:'none',
												duration:2000
											});
										}
									},
									fail: () => {
										uni.showToast({
											title: '网络错误',
											icon:'none',
											duration:2000
										});
									},
									complete: () => {}
								});
							}else if(pay.data.code==3){
								uni.hideLoading()
								uni.showToast({
									title: pay.data.msg,
									icon:'none',
									duration:2000
								});
							}else{
								uni.hideLoading()
								uni.showToast({
									title: '网络错误',
									icon:'none',
									duration:2000
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
			wxpay(){
				if(this.way==2){
					this.way=-1
				}else{
					this.way=2
				}
			},
			alipay(){
				if(this.way==10){
					this.way=-1
				}else{
					this.way=10
				}
			},
			moneyOne(){
				this.pay_money=300.00
			},
			moneyTwo(){
				this.pay_money=200.00
			},
			moneyThree(){
				this.pay_money=100.00
			},
			moneyFour(){
				this.pay_money=50.00
			},
			moneyFive(){
				this.pay_money=20.00
			},
			moneySix(){
				this.pay_money=10.00
			}
		}
	}
</script>

<style lang="scss" scoped>
	uni-page-body{
		height: 100%;
	}
	page{
		height: 100%;
	}
	.container{
		position: absolute;
		left: 0;
		bottom: 0;
		top: 0;
		right: 0;
		overflow: hidden;
		background: #f2f2f2;
		.top{
			display: flex;
			justify-content: space-between;
			// width: 100%;
			padding: 0upx 30upx;
			height: 90upx;
			background: #FFFFFF;
			margin-top: 22upx;
			font-size: 32upx;
			line-height: 90upx;
			font-family:Microsoft YaHei;
			font-weight:400;
			.num{
				font-size:30upx;
				font-family:Microsoft YaHei;
				font-weight:bold;
				color:rgba(52,52,52,1);
			}
		}
		.bottom{
			background: #FFFFFF;
			border-radius: 30upx;
			margin-top: 36upx;
			.jine{
				display: flex;
				justify-content: space-between;
				padding: 20upx 30upx;
				line-height: 66upx;
				input{
					height: 66upx;
					text-align: right;
					min-width: 600upx;
				}
				.place{
					text-align: right;
					font-size: 24upx;
					line-height: 66upx;
				}
				.company{
					font-size: 38upx;
					font-weight: bold;
				}
			}
			.choice{
				display: flex;
				margin: 0upx 30upx;
				justify-content: space-around;
				border-bottom: 2upx solid #eaeaea;
				flex-wrap: wrap;
				.item{
					font-size:24upx;
					font-family:Microsoft YaHei;
					font-weight:400;
					color:rgba(255,255,255,1);
					width: 180upx;
					text-align: center;
					line-height: 60upx;
					margin-bottom: 20upx;
					height: 60upx;
					background: #b5b5b5;
					border-radius: 10upx;
				}
				.moneyS{
					background: #fe5621;
				}
			}
			.notes{
				margin: 28upx 30upx 0;
				padding-bottom: 20upx;
				border-bottom: 2upx solid #eaeaea;
				font-size:24upx;
				font-family:Microsoft YaHei;
				font-weight:400;
				color:rgba(52,52,52,1);
			}
			.hook{
				margin: 0upx 30upx;
				// padding-bottom: 24upx;
				display: flex;
				justify-content: space-between;
				.left{
					display: flex;
					image{
						width: 50upx;
						height: 50upx;
						border-radius: 50%;
						margin: 23upx 12upx;
					}
					.title{
						line-height: 96upx;
						font-size:24upx;
						font-family:Microsoft YaHei;
						font-weight:400;
						color:rgba(52,52,52,1);
					}
				}
				.right{
					margin: 32upx 40upx 0 0;
					width: 30upx;
					height: 30upx;
					border-radius: 50%;
					border: 2upx solid #333333;
				}
				.active{
					background: #fe5621;
					border: 2upx solid #fe5621;
				}
			}
		}
		.btn{
			width: 240upx;
			height: 80upx;
			margin: 120upx auto;
			text-align: center;
			line-height: 80upx;
			background: #fe5621;
			border-radius: 10upx;
			color: #FFFFFF;
			font-size:30upx;
			font-family:Microsoft YaHei;
			font-weight:bold;
			color:rgba(254,254,254,1);
		}
	}
</style>
